package com.crm.service.saling.impl;

import com.crm.dao.saling.DispatcherDao;
import com.crm.exception.ServiceException;
import com.crm.pojo.Dispatcher;
import com.crm.pojo.Inandoutlog;
import com.crm.service.saling.DispatcherService;
import com.crm.vo.DispacherVO;
import com.crm.vo.DispatcherContractClientUserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dr5hx on 2018/8/23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DispatcherServiceImpl implements DispatcherService {
    @Autowired
    private DispatcherDao dispatcherDao;

    /**
     * 生成一个发货单
     *
     * @param dispatcher
     * @return
     * @throws ServiceException
     */
    @Override
    public Dispatcher saveDispatcher(Dispatcher dispatcher) throws ServiceException {

        try {
            return dispatcherDao.save(dispatcher);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }

    }

    //通过发货单id查询发货单
    // findOneById
    @Override
    public Dispatcher findOneById(Long id) {
        return dispatcherDao.findOne(id);
    }

    //模糊查询订单主题或发货单id
    @Override
    public List<Inandoutlog> findAllOrderBymh(String q) {
        List<Inandoutlog> l = new ArrayList<>();
        List<DispacherVO> dlist = dispatcherDao.getAllDispatchBymh("%" + q + "%");
        for (DispacherVO d : dlist) {
            Inandoutlog io = new Inandoutlog();
            io.setIoDispatherTitle(d.getContractTopic());
            io.setIoDispatherId(d.getDispatcher().getDispatcherId());
            l.add(io);
        }
        return l;
    }

    //模糊查询订单主题或发货单id
    @Override
    public List<Inandoutlog> findAllOrderBymh2() {
        List<Inandoutlog> l = new ArrayList<>();
        List<DispacherVO> dlist = dispatcherDao.getAllDispatchBymh2();
        for (DispacherVO d : dlist) {
            Inandoutlog io = new Inandoutlog();
            io.setIoDispatherTitle(d.getContractTopic());
            io.setIoDispatherId(d.getDispatcher().getDispatcherId());
            l.add(io);
        }
        return l;
    }

    /**
     * 获取发货单总数量
     *
     * @return
     */
    @Override
    public Long getdispatcherListCount() {
        return dispatcherDao.getdispatcherListCount();
    }

    @Override
    public Dispatcher getDispatcherByDispatcherId(Long dipatcherId) {
        return dispatcherDao.getDispatcherByDispatcherId(dipatcherId);
    }

    @Override
    public List<DispatcherContractClientUserInfoVO> getDispatcherContractClientUserInfoVOList(int currpage, int rows) throws ServiceException {
        try {
            PageRequest pageRequest = new PageRequest(currpage - 1, rows);
            return dispatcherDao.getDispatcherContractClientUserInfoVOList(pageRequest);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }
}
